package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.juheapi.SmsApi;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.vo.SysDictContentVo;
import ysq.xyg.sys.service.SysDictContentService;
import ysq.xyg.web.dao.WebAreaUserDao;
import ysq.xyg.web.model.po.WebAreaUser;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebOrder;
import ysq.xyg.web.model.po.WebSignUp;
import ysq.xyg.web.model.vo.WebAreaUserVo;
import ysq.xyg.web.model.vo.WebOrderChlidVo;
import ysq.xyg.web.model.vo.WebOrderVo;
import ysq.xyg.web.service.WebAreaUserService;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-活动室预约人员service实现
 *
 * @author shengQiang yu
 * @date 2019-5-15 22:39:34
 */
@Service("webAreaUserService")
public class WebAreaUserServiceImpl extends BaseServiceImpl<WebAreaUser> implements WebAreaUserService {
    @Resource
    private SysDictContentService sysDictContentService;
    @Resource
    private WebAreaUserDao webAreaUserDao;

    private WebAreaUser webAreaUser;

    @Autowired
    public void setBaseDao(WebAreaUserDao webAreaUserDao) {
        this.baseDao = webAreaUserDao;
    }


    /**
     * 分页查询
     *
     * @param webAreaUserVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-15 22:39:34
     */
    public ReturnEntity page(WebAreaUserVo webAreaUserVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebAreaUser> list = leftList(webAreaUserVo);
        PageInfo<WebAreaUser> page=new PageInfo<WebAreaUser>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webAreaUserVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-15 22:39:34
     */
    public ReturnEntity addOrUpdate(WebAreaUserVo webAreaUserVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webAreaUserVo.getId())){
            webAreaUser=new WebAreaUser(currentUser);
            isUpate=false;
        }else{
            webAreaUser=selectByPrimaryKey(webAreaUserVo.getId());
            if(ObjectUtil.isNull(webAreaUser)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webAreaUser.setUpdateTime(new Date());
            webAreaUser.setUpdateUser(currentUser.getId());
        }

        webAreaUser.setStatus(webAreaUserVo.getStatus());
        webAreaUser.setCheckTime(new Date());
        webAreaUser.setCheckRemark(webAreaUserVo.getCheckRemark());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webAreaUser);
        }else{
            num=insert(webAreaUser);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }else{
            if(BaseConstant.BASE_CHECK_STATUS_YSH.equals(webAreaUser.getStatus())){
                SmsApi.smsSend(webAreaUser.getPhone(),SmsApi.tpl_id,"");
            }
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-15 22:39:34
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebAreaUser webAreaUser=selectByPrimaryKey(id);
        if(webAreaUser!=null){
            webAreaUser.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webAreaUser.setUpdateTime(new Date());
            webAreaUser.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webAreaUser);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 导出
     *
     * @param webAreaUserVo Vo对象
     * @author shengQiang yu
     * @date 2019-5-11 10:02:08
     */
    @Override
    public void export(HttpServletResponse response, WebAreaUserVo webAreaUserVo){
        String title="文体报名信息";
        title=title+"_"+ DateUtil.getDateText(new Date(),"MMdd");
        // 导出操作
        List<WebAreaUser> list = leftList(webAreaUserVo);
        if(list!=null&&list.size()>0){
            List<SysDictContent> weekDayDictList=sysDictContentService.acceptList(new SysDictContentVo(BaseConstant.WEB_WEEK_DAY,BaseConstant.BASE_STATUS_NORMAL));
            List<SysDictContent> timeSlotDictList=sysDictContentService.acceptList(new SysDictContentVo(BaseConstant.WEB_TIME_SOLT,BaseConstant.BASE_STATUS_NORMAL));
            List<SysDictContent> classRoomDictList=sysDictContentService.acceptList(new SysDictContentVo(BaseConstant.WEB_CLASS_ROOT,BaseConstant.BASE_STATUS_NORMAL));
            for (WebAreaUser webAreaUser:list){
                SysDictContent weekDayDict=sysDictContentService.accpetDictContent(webAreaUser.getWeekDay(),weekDayDictList);
                if(weekDayDict!=null){
                    webAreaUser.setWeekDay(weekDayDict.getName());
                }
                SysDictContent timeSlotDict=sysDictContentService.accpetDictContent(webAreaUser.getTimeSlot(),timeSlotDictList);
                if(weekDayDict!=null){
                    webAreaUser.setTimeSlot(timeSlotDict.getName());
                }
                SysDictContent classRoomDict=sysDictContentService.accpetDictContent(webAreaUser.getClassRoom(),classRoomDictList);
                if(classRoomDict!=null){
                    webAreaUser.setClassRoom(classRoomDict.getName());
                }
            }
        }
        // 导出操作
        EasyPoiUtil.exportExcel(list, title, "报名信息", WebAreaUser.class, title+".xls", response);
    }
    /**
     * 获取集合
     *
     * @param webAreaUserVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-5-15 22:39:34
     */
    public List<WebAreaUser> acceptList(WebAreaUserVo webAreaUserVo){
        Example example = new Example(WebAreaUser.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webAreaUserVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webAreaUserVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webAreaUserDao.selectByExample(example);
    }
    /**
     * 查询活动室信息集合
     *
     * @param webAreaUserVo 对象
     * @return
     * @author shengQiang yu
     * @date 2019-04-26 16:59
     */
    public List<WebAreaUser> leftList(WebAreaUserVo webAreaUserVo){
        return webAreaUserDao.leftList(webAreaUserVo);
    }
}
